<?php

namespace app\api\model\stall;

use app\common\model\settings\Region as RegionModel;
use app\common\model\stall\Apply as ApplyModel;

/**
 * 摊位申请模型
 */
class Apply extends ApplyModel
{
    /**
     * 是否为申请中
     * @param $user_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author joey
     * @date 2024/3/30 14:23
     */
    public static function isApplying($user_id)
    {
        $detail = self::getdetail(['user_id' => $user_id]);
        return $detail && (int)$detail['apply_status']['value'] === 10;
    }

    /**
     * 申请状态
     * @param $user_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author joey
     * @date 2024/3/30 14:23
     */
    public static function status($user_id)
    {
        $data   = [
            ['status' => -1, 'msg' => '未申请'],
            ['status' => 10, 'msg' => '待审核'],
            ['status' => 20, 'msg' => '审核通过'],
            ['status' => 30, 'msg' => '审核驳回'],
        ];
        $detail = self::getdetail(['user_id' => $user_id]);
        if (!$detail) {
            return $data[0];
        } elseif ($detail['apply_status']['value'] == 10) {
            return $data[1];
        } elseif ($detail['apply_status']['value'] == 20) {
            return $data[2];
        } elseif ($detail['apply_status']['value'] == 30) {
            return $data[3];
        }
    }


    /**
     * 提交申请
     * @param $user
     * @param $data
     * @return bool
     * @Author joey
     * @date 2024/3/30 14:23
     */
    public function submit($user, $data)
    {

        $province_name = trim($data['province_name']);
        $city_name = trim($data['city_name']);
        $district_name = trim($data['district_name']);

        $province_id = 0;
        $city_id = 0;
        $county_id = 0;
        if(isset($data['province_id']) && !$data['province_id']){
            $province_id = (new RegionModel()) -> withoutGlobalScope() -> where('name',$province_name) -> value('id');
            $city_id = (new RegionModel()) -> withoutGlobalScope() -> where('name',$city_name) -> value('id');
            $county_id = (new RegionModel()) -> withoutGlobalScope() -> where('name',$district_name) -> value('id');
        }

        // 数据整理
        $data = [
            'user_id'      => $user['user_id'],
            'real_name'    => trim($data['real_name']),
            'mobile'       => trim($data['mobile']),
            'store_name'   => trim($data['store_name']),
            'cate_name'    => isset($data['cate_name']) ? trim($data['cate_name']) : '',
            'photo'        => isset($data['photo']) ? trim($data['photo']) : '',
            'province_id'  => $data['province_id'] ? trim($data['province_id']) :$province_id,
            'city_id'      => $data['city_id'] ? trim($data['city_id']) :$city_id,
            'county_id'    => $data['county_id']?  trim($data['county_id']) : $county_id,
            'address'      => isset($data['address']) ? trim($data['address']) : '',
            'house_number' => isset($data['house_number']) ? trim($data['house_number']) : '',
            'apply_time'   => time(),
            'app_id'       => self::$app_id,
            'longitude' => $data['longitude']??'',
            'latitude' => $data['latitude']??'',
        ];

        return $this->save($data);
    }
}
